package org.example.shixiguanli.controller;

import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.shixiguanli.model.TPracticeModel;
import org.example.shixiguanli.service.TPracticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Description:
 * Date: 2024/2/15 22:06
 */
@RestController
@RequestMapping("/practice")
public class TPracticeController extends BaseController<String, TPracticeService, TPracticeModel> {

    @Autowired
    private TPracticeService tPracticeService;

    /**
     * 提交
     *
     * @param practiceModel
     * @return
     */
    @PostMapping("/submit")
    public ApiResult submit(@RequestBody TPracticeModel practiceModel) {
        practiceModel.setStatus("0");
        practiceModel.setUserId(getUserId());
        if (ObjectUtils.isEmpty(practiceModel.getId())) {
            add(practiceModel);
        } else {
            update(practiceModel);
        }
        return ApiResult.success();
    }

    /**
     * 更新状态（1：通过/2：不通过）
     *
     * @param params
     * @return
     */
    @PostMapping("/updateStatus")
    public ApiResult updateStatus(@RequestBody Map<String, Object> params) {
        Object status = params.get("status");
        Object id = params.get("id");
        TPracticeModel practiceModel = tPracticeService.getById((String) id);
        practiceModel.setStatus((String) status);
        update(practiceModel);
        return ApiResult.success();
    }

    /**
     * 查看当前用户的实习报告
     * @param params
     * @return
     */
    @GetMapping("/getDetail")
    public ApiResult getDetail(@RequestParam Map<String,Object> params){
        JSONObject result = tPracticeService.getDetail(params);
        return ApiResult.success(result);
    }
}
